package com.five.filesync;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

import java.net.InetAddress;

public class MdnsDisovery {

    private static final String TAG = MdnsDisovery.class.getSimpleName();
    private static int MAX_RETRY_TIME = 2;

    private Context mContext;
    private String serverName;
    private OnMdnsCallback callback;
    public MdnsDisovery(Context context, String name, OnMdnsCallback callback) {
        this.mContext = context;
        this.serverName = name;
        this.callback = callback;
        initializeDiscoveryListener();
    }

    private Thread mT;

    private static final String BROADCAST_MESSAGE = "FIND_FIVE_SERVER";

    private volatile boolean mFinished = false;

    public boolean isFinished() {
        return mFinished;
    }

    public void setEnable(boolean enable) {
        if (enable) {
            mT = new Thread() {
                @Override
                public void run() {
                    int i=0;
                    while (++i <= MAX_RETRY_TIME) {
                        Log.d(TAG, "try serarch server!!! " + i);
                        UdpBroadCastUtils.RespondData respondData = UdpBroadCastUtils.sendBroadcastAndReceiveReply(mContext, BROADCAST_MESSAGE);
                        Log.d(TAG, "replay=" + respondData);
                        if (respondData != null && !TextUtils.isEmpty(respondData.respond)) {
                            try {
                                JSONObject jsonObject = new JSONObject(respondData.respond);
                                if (serverName.equals(jsonObject.get("name"))) {
                                    // found server
                                    int port = jsonObject.getInt("port");
                                    if (port != 0) {
                                        Log.d(TAG, String.format("Found server: %s:%d", respondData.addr.toString(), port));
                                        if (callback != null) {
                                            callback.onServiceDiscoverd(respondData.addr, port);
                                            mFinished = true;
                                            return;
                                        }
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        try {
                            sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    if (callback != null) {
                        callback.onServiceNotFound(serverName);
                    }
                    mFinished = true;
                }
            };
            mT.start();
        } else {
        }
    }

    private void initializeDiscoveryListener() {

    }

    public interface OnMdnsCallback {
        void onServiceDiscoverd(InetAddress addr, int port);
        void onServiceNotFound(String serverName);
    }
}
